//
//  UIViewController+LGLeakTest.m
//  CustomLeakTest
//
//  Created by yunyi on 2020/4/24.
//  Copyright © 2020 yunyi. All rights reserved.
//

#import "UIViewController+LGLeakTest.h"
#import <objc/runtime.h>
#import "NSObject+LGSwizzing.h"

const char * LGVCFLAT = "LGVCFLAT";

@implementation UIViewController (LGLeakTest)
/**
 1.交换方法
 */



+ (void)load {
    //为了防止手动调用，出现又把方法交换回来的结果
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //交换两个方法
        [self swizzleSEL:@selector(viewWillAppear:) withSEL:@selector(lg_viewWillAppear:)];
        [self swizzleSEL:@selector(viewDidDisappear:) withSEL:@selector(lg_viewWillDisAppear:)];
        
    });
}

- (void)lg_viewWillAppear:(BOOL)animate {
    [self lg_viewWillAppear:animate];
    
    //怎么知道VC是pop状态还是push状态， 可以使用runtime添加一个属性来记录
    objc_setAssociatedObject(self, LGVCFLAT, @(NO), OBJC_ASSOCIATION_ASSIGN);
    
}

- (void)lg_viewWillDisAppear:(BOOL)animate {
    [self lg_viewWillDisAppear:animate];
    if ([objc_getAssociatedObject(self, LGVCFLAT) boolValue]) {
        // 如果为YES则表示 pop的状态--》 怎么知道vc的pop的状态
        [self willDealloc];
    }
    
}
@end
